import {
	createRouter,
	createWebHashHistory,
	createWebHistory
} from 'vue-router'

import Index from '../views/index.vue'
import NotFound from '../views/404.vue'
import Login from '../views/login.vue'
import Classify from '../views/classify.vue'
import Label from '../views/label.vue'
import Archives from '../views/archives.vue'
import Text from '../views/Text.vue'
import Tourist from '../views/tourist/index.vue'
import Info from '../views/tourist/info.vue'

const routes = [{
		path: '/',
		name: 'index',
		component: Index
	},
	{
		path: '/index',
		name: 'index',
		component: Index
	},
	{
		path: '/classify',
		name: 'classify',
		component: Classify
	},
	{
		path: '/label',
		name: 'label',
		component: Label
	},
	{
		path: '/archives',
		name: 'archives',
		component: Archives
	},
	{
		path: '/login',
		name: 'login',
		component: Login
	},
	{
		path: '/tourist',
		name: 'tourist',
		component: Tourist
	},
	{
		path: '/tourist/info',
		name: 'info',
		component: Info,
		beforeEnter: (to, from, next) => {
			authenticationTourist(to, from, next);
		}
	},
	{
		path: '/text',
		name: 'text',
		component: Text
	},
	{
		path: '/:path(.*)',
		name: '404',
		component: NotFound
	}
]
/**
 * 验证游客身份
 * @param {Object} to
 * @param {Object} from
 * @param {Object} next
 */
function authenticationTourist(to, from, next){
	console.log('我是路由独享守卫！！！')
	console.log(to)
	console.log(from)
	let isLogin = window.sessionStorage.getItem('userInfo');
	if (isLogin) {
		//如果用户信息存在则往下执行。
		next()
	} else {
		next('/login')
	}
}

const router = new createRouter({
	history: createWebHistory(),
	routes
})

export default router;
